package com.estar.assistsurvy.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.apache.http.util.EncodingUtils;

import android.os.Environment;
import android.util.Log;

public class FileUtils {

	// 将字符串写入到文本文件中
	public static void writeTxtToFile(String strcontent, String filePath, String fileName) {
		//生成文件夹之后，再生成文件，不然会出错
		makeFilePath(filePath, fileName);

		String strFilePath = filePath+fileName;
		// 每次写入时，都换行写
		String strContent = strcontent + "\r\n";
		try {
			File file = new File(strFilePath);
			if (!file.exists()) {
				Log.d("TestFile", "Create the file:" + strFilePath);
				file.getParentFile().mkdirs();
				file.createNewFile();
			}
			RandomAccessFile raf = new RandomAccessFile(file, "rwd");
			raf.seek(file.length());
			raf.write(strContent.getBytes());
			raf.close();
		} catch (Exception e) {
			Log.e("TestFile", "Error on write File:" + e);
		}
	}

	// 生成文件
	public static File makeFilePath(String filePath, String fileName) {
		File file = null;
		makeRootDirectory(filePath);
		try {
			file = new File(filePath + fileName);
			if (!file.exists()) {
				file.createNewFile();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return file;
	}

	// 生成文件夹
	public static void makeRootDirectory(String filePath) {
		File file = null;
		try {
			file = new File(filePath);
			if (!file.exists()) {
				file.mkdir();
			}
		} catch (Exception e) {
			Log.i("error:", e+"");
		}

	}

	//读SD中的文件  
	public static String readFileSdcardFile(String fileName) throws IOException{   
		String res="";   
		try{   
			FileInputStream fin = new FileInputStream(fileName);   

			int length = fin.available();   

			byte [] buffer = new byte[length];   
			fin.read(buffer);       

			res = EncodingUtils.getString(buffer, "UTF-8");   

			fin.close();       
		}   

		catch(Exception e){   
			e.printStackTrace();   
		}   
		return res;   
	}

	/**
	 * 将文件转为byte[]
	 * @param filePath 文件路径
	 * @return
	 */
	public static byte[] getBytes(String filePath){
		File file = new File(filePath);
		ByteArrayOutputStream out = null;
		try {
			FileInputStream in = new FileInputStream(file);
			out = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			@SuppressWarnings("unused")
			int i = 0;
			while ((i = in.read(b)) != -1) {

				out.write(b, 0, b.length);
			}
			out.close();
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		byte[] s = out.toByteArray();
		return s;

	}
	
	public static File getPhotoFile(){
		try {
			// 获取 SD 卡根目录
			String saveDir = Environment
					.getExternalStorageDirectory()
					+ "/DCIM/";
			// 新建目录
			File dir = new File(saveDir);
			if (! dir.exists()) dir.mkdir();
			// 生成文件名
			long currentTime = System.currentTimeMillis();
			String filename = "ESTAR" + currentTime + ".jpg";
			// 新建文件
			File file = new File(saveDir, filename);
			// 打开文件输出流
			//			fileOutputStream = new FileOutputStream(file);
			// 生成图片文件
			//			this.photo.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
			// 相片的完整路径
			return file;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
